import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/api/odoo/sale_bid_result_odoo_api.dart';
import 'package:flutter_admin/models/odoo/sale_bid_result.dart';
import 'package:flutter_admin/pages/common/cry/cry_all.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/pages/common/cry/form/cry_input.dart';
import 'package:flutter_admin/pages/common/cry/form/cry_select_date.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/pages/common/cry/utils/cry_utils.dart';
import 'package:flutter_admin/utils/utils.dart';
import '../../../generated/l10n.dart';

class SaleBidResultEdit extends StatefulWidget {
  String? projectId;
  SaleBidResultEdit({this.saleBidResult, this.projectId});

  final SaleBidResult? saleBidResult;

  @override
  _SaleBidResultEditState createState() => _SaleBidResultEditState();
}

class _SaleBidResultEditState extends State<SaleBidResultEdit> {
  late SaleBidResult saleBidResult;
  bool? isAdd;

  final GlobalKey<FormState> formKey = GlobalKey<FormState>();
  ValueNotifier<num> bidamount = ValueNotifier<num>(0);
  ValueNotifier<num> bidresult = ValueNotifier<num>(0);

  @override
  void initState() {
    super.initState();
    this.isAdd = widget.saleBidResult == null;
    this.saleBidResult = widget.saleBidResult ?? SaleBidResult();
    if (isAdd == true) {
      this.saleBidResult.biddate = Utils.getDate();
    }
    this.saleBidResult.projectId = widget.saleBidResult != null
        ? widget.saleBidResult!.projectId
        : widget.projectId;
    _loadSelectVO();
  }

  _loadSelectVO() async {
    setState(() {});
  }

  _getBidamount() {
    bidamount.value =
        saleBidResult.projectbudget! * (saleBidResult.discountfactor! * 0.01);
    bidamount.value = num.parse(bidamount.value.toStringAsFixed(2));
    saleBidResult.bidamount = bidamount.value;
    _getBidresult();
  }

  _getBidresult() {
    bidresult.value =
        saleBidResult.bidamount! * (saleBidResult.bidshare! * 0.01);
    bidresult.value = num.parse(bidresult.value.toStringAsFixed(2));
  }

  @override
  Widget build(BuildContext context) {
    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          // CryInputNum(
          //   label: '招投标项目Id',
          //   value: saleBidResult.projectId,
          //   onSaved: (v) {
          //     saleBidResult.projectId = v;
          //   },
          //   validator: (v) {
          //     return v!.isEmpty
          //         ? S.of(context).required
          //         : v == '0'
          //             ? '招投标项目Id不能为0'
          //             : int.parse(v) > 100
          //                 ? '招投标项目Id小于等于100'
          //                 : null;
          //   },
          // ),
          CrySelectDate(
            context,
            label: '投标日期',
            value: saleBidResult.biddate,
            onSaved: (v) {
              saleBidResult.biddate = v;
            },
          ),
          CryInputNum(
            label: '*项目预算（万元）',
            value: saleBidResult.projectbudget,
            onSaved: (v) {
              saleBidResult.projectbudget = v;
            },
            onChange: (v) {
              formKey.currentState?.save();
              _getBidamount();
            },
            validator: (v) {
              return v!.isEmpty
                  ? S.of(context).required
                  : v == '0'
                      ? '项目预算（万元）不能为0'
                      // : int.parse(v) > 100
                      //     ? '项目预算（万元）小于等于100'
                      : null;
            },
          ),
          CryInput(
            label: '*中标厂家',
            value: saleBidResult.bidmanufactor,
            onSaved: (v) {
              saleBidResult.bidmanufactor = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          // CryInputNum(
          //   label: '中标金额（万元）',
          //   enable: false,
          //   value: saleBidResult.bidamount,
          //   onSaved: (v) {
          //     saleBidResult.bidamount = v;
          //   },
          //   onChange: (v) {
          //     formKey.currentState?.save();
          //     _getBidresult();
          //   },
          // ),
          ValueListenableBuilder<num>(
            valueListenable: bidamount,
            builder: (context, value, child) {
              return CryInputNum(
                label: '中标金额（万元）',
                enable: false,
                value: bidamount.value,
                // onChange: (v) {
                //   formKey.currentState?.save();
                //   _getBidresult();
                // },
                onSaved: (v) {
                  saleBidResult.bidamount = v;
                },
              );
            },
          ),
          CryInputNum(
            label: '*中标份额（%）',
            value: saleBidResult.bidshare,
            onSaved: (v) {
              saleBidResult.bidshare = v;
            },
            onChange: (v) {
              formKey.currentState?.save();
              _getBidresult();
            },
            validator: (v) {
              return v!.isEmpty
                  ? S.of(context).required
                  : v == '0'
                      ? '中标份额（%）不能为0'
                      // : int.parse(v) > 100
                      //     ? '中标份额（%）小于等于100'
                      : null;
            },
          ),
          CryInputNum(
            label: '*折扣系数（%）',
            value: saleBidResult.discountfactor,
            onSaved: (v) {
              saleBidResult.discountfactor = v;
            },
            onChange: (v) {
              formKey.currentState?.save();
              _getBidamount();
            },
            validator: (v) {
              return v!.isEmpty
                  ? S.of(context).required
                  : v == '0'
                      ? '折扣系数（%）不能为0'
                      : int.parse(v) > 100
                          ? '折扣系数（%）小于等于100'
                          : null;
            },
          ),
          ValueListenableBuilder<num>(
            valueListenable: bidresult,
            builder: (context, value, child) {
              return CryInputNum(
                label: '结果（万元）',
                enable: false,
                value: bidresult.value,
                onSaved: (v) {
                  saleBidResult.bidresult = v;
                },
              );
            },
          ),
          CryInput(
            label: '*价格评分办法',
            value: saleBidResult.priceScoring,
            onSaved: (v) {
              saleBidResult.priceScoring = v;
            },
          ),
          CryInputNum(
            label: '*我司报价',
            value: saleBidResult.ourQuotation,
            onSaved: (v) {
              saleBidResult.ourQuotation = v;
            },
            onChange: (v) {
              formKey.currentState?.save();
            },
            validator: (v) {
              return v!.isEmpty
                  ? S.of(context).required
                  : v == '0'
                      ? '我司报价不能为0'
                      // : int.parse(v) > 100
                      //     ? '项目预算（万元）小于等于100'
                      : null;
            },
          ),
          CryInputNum(
            label: '*靶心价',
            value: saleBidResult.blankPrice,
            onSaved: (v) {
              saleBidResult.blankPrice = v;
            },
            onChange: (v) {
              formKey.currentState?.save();
            },
            validator: (v) {
              return v!.isEmpty
                  ? S.of(context).required
                  : v == '0'
                      ? '靶心价不能为0'
                      : null;
            },
          ),
          CryInputNum(
            label: '*最高价',
            value: saleBidResult.highestPrice,
            onSaved: (v) {
              saleBidResult.highestPrice = v;
            },
            onChange: (v) {
              formKey.currentState?.save();
            },
            validator: (v) {
              return v!.isEmpty
                  ? S.of(context).required
                  : v == '0'
                      ? '最高价不能为0'
                      : null;
            },
          ),
          CryInputNum(
            label: '*最低价',
            value: saleBidResult.minimumPrice,
            onSaved: (v) {
              saleBidResult.minimumPrice = v;
            },
            onChange: (v) {
              formKey.currentState?.save();
            },
            validator: (v) {
              return v!.isEmpty
                  ? S.of(context).required
                  : v == '0'
                      ? '最低价不能为0'
                      : null;
            },
          ),
          CryInput(
            label: '备注',
            value: saleBidResult.remark,
            onSaved: (v) {
              saleBidResult.remark = v;
            },
          ),
        ],
      ),
    );
    var buttonBar = ButtonBar(
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        CryButton(
          label: S.of(context).save,
          onPressed: () {
            FormState form = formKey.currentState!;
            if (!form.validate()) {
              return;
            }
            form.save();
            SaleBidResultOdooApi.saveOrUpdate(saleBidResult.toOdooMap())
                .then((ResponseBodyApi res) {
              if (!res.success!) {
                //package:flutter_admin/pages/common/cry(re s.message!);
                return;
              }
              Navigator.pop(context, true);
              CryUtils.message(S.of(context).saved);
            });
          },
          iconData: Icons.save,
        ),
        CryButton(
          label: S.of(context).cancel,
          onPressed: () {
            Navigator.pop(context);
          },
          iconData: Icons.cancel,
        )
      ],
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text(this.isAdd! ? S.of(context).add : S.of(context).modify),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [form],
        ),
      ),
      bottomNavigationBar: buttonBar,
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
    // return result;
  }
}
